<?php
/**
 * @version	$Id: holding.php 20 2011-02-10 00:35:39Z gregdaynes@gmail.com $
 * @package	jEvolve.syncr
 * @copyright  Copyright (C) 2010 jEvolve, LLC. All rights reserved.
 * @license	GNU General Public License
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

class SyncrModelHolding extends JModel
{
	var $_data	= null;
	
	public function &getData()
	{
		if (empty($this->_data))
		{
			// pagination
			$pagination =& $this->getPagination();
			// query
			$query = $this->_buildQuery();
			// data
			$this->_data = $this->_getList($query, $pagination->limitstart, $pagination->limit);
			
			if ($this->_data) {
				foreach($this->_data as $index=>$item) {
					$data = json_decode($item->data);
					$title = $data->title;
					
					$this->_data[$index]->title = $title;
				}
			}
			
			return $this->_data;
		}
	}
	
	public function &getPagination()
	{
		// pagination library
		jimport('joomla.html.pagination');
		
		$app =& JFactory::getApplication();
		
		// pagination
		$this->_pagination = new JPagination($this->getTotal(), JRequest::getVar('limitstart', 0), JRequest::getVar('limit', $app->getCfg('list_limit')));
		
		$this->_pagination->_viewall = false;
		
		return $this->_pagination;
	}
	
	public function getTotal()
	{
		$query = $this->_buildQuery();
		
		$this->_total = $this->_getListCount($query);
		
		return (int) $this->_total;
	}
	
	private function _buildQuery()
	{
		$db =& JFactory::getDBO();
		
		$this->_query = 'SELECT h.id'
					 .', h.data'
					 .', h.original'
					 .', h.category'
					 .', c.title AS category_title'
					 .' FROM #__syncr_hold AS h'
					 .' LEFT JOIN #__categories AS c ON h.category = c.id'
					 . $this->_buildQueryWhere()
					 . $this->_buildQueryOrderBy()
					 ;
		return (string) $this->_query;
	}
	
	private function _buildQueryOrderBy()
	{
		$app	=& JFactory::getApplication();
		$db		=& JFactory::getDBO();
		
		// user state
		$order	= $app->getUserStateFromRequest('com_syncr.filterOrder', 'filter_order', 'id', 'word');
		if ($order !== 'id'
		 && $order !== 'process_id'
		 && $order !== 'action'
		 ) {
			$order = 'id';
		}
		
		$orderDirection = $app->getUserStateFromRequest('com_syncr.filterOrderDirection', 'filter_order_Dir', 'ASC', 'cmd');
		$orderDirection = strtoupper($orderDirection);
		if ($orderDirection !== 'ASC'
		 && $orderDirection !== 'DESC'
		 ) {
		 	$orderDirection = 'DESC';
		}
		
		return (string) ' ORDER BY '.$db->nameQuote($order).' '.$orderDirection.' ';
	}
	
	private function _buildQueryWhere()
    {
    	// load joomla application
    	$app =& JFactory::getApplication();

    	// get variables from user state
    	$filter_section		= $app->getUserStateFromRequest('com_syncr.filter_section',	'filter_section',	'', 'id');
    	$filter_category	= $app->getUserStateFromRequest('com_syncr.filter_category',	'filter_category',	'', 'id');
				
    	// define where
    	$where = null;

		// section
		if ($filter_section) {
			$where[] = 'h.t_section = '.$filter_section;
		}
		
		// category
		if ($filter_category) {
			$where[] = 'h.t_category = '.$filter_category;
		}

    	// if where array has contents
    	// implode into where string
    	if (count($where)) {
    		$where = ' WHERE ' . implode(' AND ', $where);
    	}

    	// return where string
    	return (string) $where;
    }
}